﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Background.Core.DAL;
using System.Xml;
using Background.Core.Entity;
using System.Xml.Xsl;
using System.Text;
using System.IO;
using Background.Core.Events;
namespace Background.Core.Controllers
{
    /// <summary>
    /// 统一后台管理系统
    /// </summary>
    public class AdminController : Commons.Web.BaseController
    {
        WebManageMenusRepository webManageMenusRepository;
        WebManageUsersRepository webManageUsersRepository;
        public AdminController()
        {
            webManageMenusRepository = new WebManageMenusRepository();
            webManageUsersRepository = new WebManageUsersRepository();
        }
        #region 登陆&登出&航导相关
        public ActionResult Index()
        {
            var logon = new LogOnEvent(); //存储一个登陆事件对象
            Session["LogOnEvent"] = logon;
            logon.LogOnAfterFunc += () =>
            {
                return new ContentResult
                {
                    Content = "<script>location.href='http://www.sina.com';</script>",
                    ContentType = "text/html",
                };
            };//订阅这个登陆事件
            return View();
        }

        /// <summary>
        /// 顶部菜单树
        /// </summary>
        /// <returns></returns>
        public ActionResult Top()
        {
            return PartialView(webManageMenusRepository.GetWebManageMenusByLevel(1));
        }


        /// <summary>
        /// 左侧菜单树
        /// </summary>
        /// <returns></returns>
        public ActionResult Left(int? menuID = WebManageMenus.ROOTID)
        {
            var html = new StringBuilder();
            var model = webManageMenusRepository.GetEntity<WebManageMenus>(i => i.MenuID == menuID);
            GetTreeMenu(html, model);
            ViewBag.html = html.ToString();
            return PartialView();
        }
        /// <summary>
        /// 得到树形结构
        /// </summary>
        /// <param name="html"></param>
        /// <param name="webmanagemenu"></param>
        private void GetTreeMenu(StringBuilder html, WebManageMenus webmanagemenu)
        {
            html.Append("<li class='menu-item'>");
            html.Append("<a read='" + webmanagemenu.Status + "' href='" + webmanagemenu.LinkUrl + "' target=\"MainFrm\">");
            html.Append(webmanagemenu.MenuName);
            html.Append("</a>");
            List<WebManageMenus> childmanagemenus = webManageMenusRepository.GetWebManageMenusByParentID(webmanagemenu.MenuID);
            if (childmanagemenus != null && childmanagemenus.Count > 0)
            {
                html.Append("<ul>");
                foreach (var item in childmanagemenus)
                {
                    this.GetTreeMenu(html, item);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOn()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LogOn(FormCollection form)
        {
            var user = webManageUsersRepository.UserLogin(new WebManageUsers
            {
                LoginName = form["LoginName"],
                Password = VCommons.Encryptor.Utility.EncryptString(form["Password"], VCommons.Encryptor.Utility.EncryptorType.MD5),
            });
            if (user != null)
            {
                //TODO:登陆成功的逻辑
                Session.Add("UserID", user.ManageUserID);
                Session.Add("UserName", user.RealName);
                #region 触发统一的事件，它可以由不同页面去订阅
                var logOnEvent = Session["LogOnEvent"] as LogOnEvent;
                if (logOnEvent != null)
                    return logOnEvent.OnLogOnAfterFunc();
                #endregion

            }
            else
            {
                //TODO:登陆失败的逻辑
            }
            return View();
        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public ContentResult LogOut()
        {
            Session.Clear();
            return new ContentResult
            {
                ContentType = "text/html",
                Content = "<script>alert('成功登出');location.href='/Admin/Logon';</script>",
            };
        }
        #endregion
    }
}
